package cn.com.dhc.danlu.widget.loadlayer;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com) Date: 2016-01-15 Time: 15:39
 * Description: 视图切换布局 usagetips: 最外层xml嵌套当前组合空间类
 * Time: 15:39
 * usagetips:  最外层xml嵌套当前组合空间类
 * TODO 目前实现较为简单 visible、gone 后期可以实现状态切换过程中淡入淡出的动画效果
 */
public class LoaddingLayout extends FrameLayout {
    private View mEmptyView;
    private View mErrorView;
    private View mCurrentView;
    private View mLoaddingView;

    public LoaddingLayout(Context context) {
        this(context, null);
    }

    public LoaddingLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoaddingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Danlu_Loaddinglayout, defStyleAttr, 0);
        int emptyViewId = 0;
        int errorViewId = 0;
        int loaddingViewId = 0;
        for (int i = 0; i < ta.getIndexCount(); i++) {
            int index = ta.getIndex(i);
            if (index == R.styleable.Danlu_Loaddinglayout_empty_view_layout) {
                emptyViewId = ta.getResourceId(index, 0);
            } else if (index == R.styleable.Danlu_Loaddinglayout_error_view_layout) {
                errorViewId = ta.getResourceId(index, 0);
            } else if (index == R.styleable.Danlu_Loaddinglayout_loadding_view_layout) {
                loaddingViewId = ta.getResourceId(index, 0);
            }
        }
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        if (0 != emptyViewId) {
            mEmptyView = layoutInflater.inflate(emptyViewId, null, true);
        } else {
            // use default
            mEmptyView = layoutInflater.inflate(R.layout.empty_view, null, true);
        }
        if (0 != errorViewId) {
            mErrorView = layoutInflater.inflate(errorViewId, null, true);
        } else {
            // use default
            mErrorView = layoutInflater.inflate(R.layout.error_view, null, true);
        }
        if (0 != loaddingViewId) {
            mLoaddingView = layoutInflater.inflate(loaddingViewId, null, true);
        } else {
            // use default
            mLoaddingView = layoutInflater.inflate(R.layout.loading_view, null, true);
        }
        addView(mLoaddingView);
        addView(mErrorView);
        addView(mEmptyView);
        this.mCurrentView = mLoaddingView;
        ta.recycle();
    }

    public void changeEmptyRetryText(String string) {
        if (!TextUtils.isEmpty(string)) {
            Button tv_error = (Button) mEmptyView.findViewById(R.id.btn_reload);
            tv_error.setText(string);
        }
    }

    public void changeEmptyViewTips(String tips) {
        if (!TextUtils.isEmpty(tips)) {
            TextView tv_empty = (TextView) mEmptyView.findViewById(R.id.tv_error_info);
            tv_empty.setText(tips);
        }
    }

    public void isShowEmptyRetryBtn(boolean isShow) {
        mEmptyView.findViewById(R.id.btn_reload).setVisibility(isShow ? VISIBLE : GONE);
    }

    @Deprecated
    public void isHidenInteraction(boolean isHiden) {
        mEmptyView.findViewById(R.id.btn_reload).setVisibility(isHiden ? View.GONE : View.VISIBLE);
        mErrorView.findViewById(R.id.btn_error_retry).setVisibility(isHiden ? View.GONE : View.VISIBLE);
    }

    public void setEmptyPic(@DrawableRes int picResid) {
        if (null != mEmptyView) {
            ImageView ivEmpty = (ImageView) mEmptyView.findViewById(R.id.iv_error_info);
            ivEmpty.setImageResource(picResid);
        }
    }

    public View getCurrentView() {
        return mCurrentView;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount() - 1; i++) {
            View childView = getChildAt(i);
            childView.setVisibility(GONE);
        }
        showLoaddingView();
    }

    public void showLoaddingView() {
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView == mLoaddingView) {
                childView.setVisibility(VISIBLE);
                mCurrentView = mLoaddingView;
            } else {
                childView.setVisibility(GONE);
            }
        }
    }

    public void setRetryClickListener(final OnClickListener listener) {
        setEmptyViewRetryClickListener(listener);
        setErrorViewRetryClickListener(listener);
    }

    public void setEmptyViewRetryClickListener(final OnClickListener listener) {
        View emptyRetryView = mEmptyView.findViewById(R.id.btn_reload);
        if (null != emptyRetryView) {
            emptyRetryView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onClick(v);
                    }
                }
            });
        }
    }

    public void setErrorViewRetryClickListener(final OnClickListener listener) {
        View errorRetryView = mErrorView.findViewById(R.id.btn_error_retry);
        if (null != errorRetryView) {
            errorRetryView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onClick(v);
                    }
                }
            });
        }
    }

    public void showEmptyView() {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(GONE);
        }
        mEmptyView.setVisibility(VISIBLE);
        mCurrentView = mEmptyView;
    }

    public void showEmptyView(String info) {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(GONE);
        }
        mEmptyView.setVisibility(VISIBLE);
        ((TextView) mEmptyView.findViewById(R.id.tv_error_info)).setText(info);
        mCurrentView = mEmptyView;
    }

    public void showErrorView() {
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView == mErrorView) {
                childView.setVisibility(VISIBLE);
                mCurrentView = childView;
            } else {
                childView.setVisibility(GONE);
            }
        }
    }

    public void showErrorView(String info) {
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView == mErrorView) {
                childView.setVisibility(VISIBLE);
                ((TextView) mErrorView.findViewById(R.id.tv_error_info)).setText(info);
                mCurrentView = childView;
            } else {
                childView.setVisibility(GONE);
            }
        }
    }

    public void showContentView() {
        if (isSuccessView()) {
            return;
        }
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView != mEmptyView && childView != mErrorView && childView != mLoaddingView) {
                childView.setVisibility(VISIBLE);
                mCurrentView = childView;
            } else {
                childView.setVisibility(GONE);
            }
        }
    }

    public boolean isSuccessView() {
        return mCurrentView != mEmptyView && mCurrentView != mErrorView && mCurrentView != mLoaddingView;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    /**
     * activity destroy call this method
     */
    public void releaseCacheView() {
        this.removeAllViews();
        this.mLoaddingView = null;
        this.mErrorView = null;
        this.mEmptyView = null;
    }
}